package com.example.roundcornerprogressbar.view.progressbar;

import com.example.roundcornerprogressbar.ResourceTable;
import com.example.roundcornerprogressbar.view.progressbar.common.AnimatedRoundCornerProgressBar;
import ohos.agp.components.AttrSet;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

public class RoundCornerProgressBar extends AnimatedRoundCornerProgressBar {
    public RoundCornerProgressBar(Context context) {
        super(context);
    }

    public RoundCornerProgressBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public RoundCornerProgressBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    protected int initLayout() {
        return ResourceTable.Layout_layout_round_corner_progress_bar;
    }

    @Override
    protected void initStyleable(Context context, AttrSet attrs) {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void drawProgress(DirectionalLayout layoutProgress,
                                ShapeElement progressDrawable,
                                float max,
                                float progress,
                                float totalWidth,
                                int radius,
                                int padding,
                                boolean isReverse) {
        int newRadius = radius - (padding / 2);
        progressDrawable.setCornerRadiiArray(new float[]{newRadius, newRadius, newRadius, newRadius, newRadius, newRadius, newRadius, newRadius});
        layoutProgress.setBackground(progressDrawable);
        float ratio = max / progress;
        int progressWidth = (int) ((totalWidth - (padding * 2)) / ratio);
        ComponentContainer.LayoutConfig progressParams = (ComponentContainer.LayoutConfig)layoutProgress.getLayoutConfig();
        progressParams.width = progressWidth;
        if (padding + (progressWidth / 2) < radius) {
            int margin = Math.max(radius - padding, 0) - (progressWidth / 2);
            progressParams.setMarginTop(margin);
            progressParams.setMarginBottom(margin);
        } else {
            progressParams.setMarginTop(0);
            progressParams.setMarginBottom(0);
        }
        layoutProgress.setLayoutConfig(progressParams);
    }

    @Override
    protected void onViewDraw() {

    }
}
